using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


enum CardState
{
    YES,
    OR,
    NO
}
public enum PlantType
{
    XRK,
    WDSS
}

public class Card : MonoBehaviour
{
    private CardState cardstate = CardState.OR;
    public PlantType plantType = PlantType.XRK;
    public GameObject cardyes;
    public GameObject cardno;
    public Image cardor;
    public float cdtime = 2;
    private float cdtimer = 0;
    public int needSunPoint = 50;
    private void Update()
    {
        switch (cardstate)
        {
            case CardState.YES:
                YesUpdate();
                break;
            case CardState.OR:
                OrUpdate();
                break;
            case CardState.NO:
                NoUpdate();
                break;
        }
    }
    void YesUpdate ()
    {
        if (needSunPoint > SunNumber.Instance.SunPoint)
        {
            ToNo();
        }
    }
    void OrUpdate ()
    {
        cdtimer += Time.deltaTime;
        cardor.fillAmount = (cdtime - cdtimer) / cdtime;
        if (cdtimer >= cdtime)
        {
            ToNo();
        }
    }
    void NoUpdate()
    {
        if(needSunPoint <= SunNumber.Instance.SunPoint)
        {
            ToYes();
        }
    }
    void ToNo()
    {
        cardstate = CardState.NO;
        cardyes.SetActive(false);
        cardno.SetActive(true);
        cardor.gameObject.SetActive(false);
    }
    void ToYes()
    {
        cardstate = CardState.YES;
        cardyes.SetActive(true);
        cardno.SetActive(false);
        cardor.gameObject.SetActive(false);
    }
    void ToOr()
    {
        cardstate = CardState.OR;
        cdtimer = 0;
        cardyes.SetActive(false);
        cardno.SetActive(true);
        cardor.gameObject.SetActive(true);
    }
    public void OnClick()
    {
        if (needSunPoint > SunNumber.Instance.SunPoint) return;
        bool isplnat = Hand.instance.ADDPlant(plantType);
        if (isplnat)
        {
            SunNumber.Instance.SubSun(needSunPoint);
            ToOr();
        }
    }
}
